package com.flowersinc.shop.views
{
	import com.flowersinc.shop.model.Flower;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.DataGrid;
	import mx.controls.Image;
	import mx.events.DragEvent;
	import mx.managers.DragManager;

	public class ShoppingCartBase extends Canvas
	{
		
		[Bindable]
		public var customerDataGrid : DataGrid;
		public var flowers : ArrayCollection;
		
		[Bindable]
		protected var flowersCount : Number = 0;
		
		[Bindable]
		protected var orders : ArrayCollection = new ArrayCollection();
		
		
		[Bindable(event="flowerAdded")] 
		[Bindable(event="flowerRemoved")]
		protected function get price() : Number 
		{
			var totalPrice : Number = 0;
			
			for each (var flower : Flower in orders) 
			{	
				totalPrice += flower.amount * flower.price;
			}	
			
			return totalPrice;
		}
		
		public function addToCart(flower:Flower):void
		{
			flowersCount += 1;
			
			var foundFlower : Boolean = false;
			
			for each (var item : Flower in orders) 
			{
				if (Flower(flower).name == item.name) 
				{
					foundFlower = true;
					item.amount += 1;
					flower.amount -= 1;
				}
			}
			
			flower.canOrder = (flower.amount > 0);	 	
			
			if (foundFlower == false) 
			{
				var orderedFlowerInstance : Flower = new Flower();
				orderedFlowerInstance.name = flower.name;
				orderedFlowerInstance.addEventListener("itemDeleted", onItemDeleted);		
				orderedFlowerInstance.amount = 1;
				orderedFlowerInstance.price = flower.price;
				flower.amount -= 1;
					
				orders.addItem(orderedFlowerInstance);
				
				for (var i : int = 0; i < orders.length; i++) 
				{
					Flower(orders[i]).number = i + 1; 
				}
			}
			
			dispatchEvent(new Event("flowerAdded"));
		}
	
		protected function onCartButtonClick() : void 
		{
			if (currentState == null) 
			{
				currentState = "HideDetails";
			}
			else 
			{
				currentState = null;
			}
		}
		
		protected function onDragEnter(event:DragEvent):void
		{	
			var flower : Flower = event.dragSource.dataForFormat('flower') as Flower;
			
			if (flower !== null)
				var dropTarget : DataGrid = event.currentTarget as DataGrid;
				DragManager.acceptDragDrop(dropTarget);
		}
		
		protected function onDragDrop(event:DragEvent):void
		{
			var flower : Flower = event.dragSource.dataForFormat('flower') as Flower;
			addToCart(flower);
		}
			
		
		private function onItemDeleted(event : Event) : void 
		{	
			for each (var item : Flower in flowers) 
			{
				if (Flower(event.target).name == item.name) 
				{
					item.amount += 1;
					event.target.amount -= 1;
					break;
				}
			}
			
			item.canOrder = (item.amount > 0);
			
			if (event.target.amount == 0) 
			{ 
				orders.removeItemAt(orders.getItemIndex(customerDataGrid.selectedItem)); 
			}
			
			for (var i : int = 0; i < orders.length; i++) 
			{
				Flower(orders[i]).number = i + 1; 
			}
			
			flowersCount -= 1;
			
			dispatchEvent(new Event("flowerRemoved"));
		}
	}
}